a. Install and load the following packages:
tidyversestringrplotlycrosstalkhtmltoolsDTb. Import the ACS dataset into your R Markdown or R Notebook.
c. Clean the dataset using the
clean_acs_metadata() function provided below.
d. Explore the data by answering the following questions:
clean_acs_metadata <- function(df) {
df_clean <- df %>%
rename(variable = name) %>%
mutate(
label_clean = label %>%
str_remove("^Estimate!!") %>%
str_replace_all("!!", "_") %>%
str_replace_all("[^A-Za-z0-9_ ]", "") %>%
str_replace_all(" +", "_") %>%
str_to_lower(),
concept_clean = concept %>%
str_to_lower() %>%
str_replace_all("[^a-z0-9 ]", "") %>%
str_replace_all(" +", "_"),
varname = paste0(concept_clean, "_", label_clean),
) %>%
select(geoid, state, varname, estimate) %>%
pivot_wider(
id_cols = c(geoid, state),
names_from = varname,
values_from = estimate
) %>%
relocate(geoid, state) %>%
select(-`NA_NA`)
return(df_clean)
}
a. Create new percentage race variables:
pct_whitepct_blackpct_asianpct_hispb. Create percentage housing tenure variables:
pct_ownerpct_renterc. Create age bucket variables (using the code provided below):
age_children (0–17)age_adults (18–64)age_seniors (65+)# Age groups are extremely granular. Bucket them into children (0 - 17), adult (18 - 64), and seniors (65 +)
acs_wide <- acs_wide %>%
mutate(
age_children = rowSums(
select(., matches("sex_by_age_total_(male|female)_(under_5|5_to_9|10_to_14|15_to_17)_years$")),
na.rm = TRUE
),
age_adults = rowSums(
select(., matches("sex_by_age_total_(male|female)_(18_and_19|20|21|22_to_24|25_to_29|30_to_34|35_to_39|40_to_44|45_to_49|50_to_54|55_to_59|60_and_61|62_to_64)_years$")),
na.rm = TRUE
),
age_seniors = rowSums(
select(., matches("sex_by_age_total_(male|female)_(65_and_66|67_to_69|70_to_74|75_to_79|80_to_84|85_years_and_older)")),
na.rm = TRUE
)
)
Create a filter_select() that filters the dataset by state.
a. How does the filter_select() know which rows to include/exclude?
b. Why does it only work when the input is a SharedData object?
Using the same sd SharedData object:
a. Create a Plotly bar chart that displays at least two racial groups.
b. Place the chart next to your state filter using
the bscols() layout function.
c. Add a simple HTML summary box that shows a reactive metric (e.g., percent seniors).
See the example below.